﻿$(function () {
    var pickerOpts = {
        changeMonth: true,
        changeYear: true,
        dateFormat: "yy/mm/dd",
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        firstDay: 0,
        nextText: "下一月",
        prevText: "上一月",
        monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        isRTL: false,
        showButtonPanel: false,
        yearRange: "-25:+25"
    }
    $('#startTime').datepicker(pickerOpts);
    $('#endTime').datepicker(pickerOpts);
    $("tr.AlternatingRowStyle").live('dblclick', function () {
        var ID = $.trim($(this).children().first().html());
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        window.open("/ITService/views?servicerequestid=" + ID + "&nocache=" + rnd, "_self");
    });
    $.ajax({
        url: "../WorkList/getAllServiceList",
        type: "GET",
        dataType: "json",
        success: function (data) {
            $.each(data, function (i, item) {
                $("#serviceType").append("<option value=" + item.serviceTypeID + ">" + item.serviceName + "</option>");
            });
        }
    });

    $("#search_submit").bind("click", function () {
        $("input#index").val(1);
        getDoneWorkList(1);
    });

});

function getDoneWorkList(stype) {
    var dater = (new Date()).toString() + Math.random() * 999;
    $.ajax({
        url: "getDoneList_By_params?r=" + dater,
        type: "GET",
        data: { "requestID": $("input#requestID").val(),
            "serviceTypeID": $("select#serviceType option:selected").val(),
            "userName": $("input#approveOwner").val(),
            "description": $("input#description").val(),
            "status": $("select#status option:selected").val(),
            "startTime": $("input#startTime").val(),
            "endTime": $("input#endTime").val(),
            "ps": parseInt($("input#size").val(), 10),
            "pi": parseInt($("input#index").val(), 10)
        },
        dataType: "json",
        success: function (data) {
            $("#List").empty();
            $("#List").append("<tr><th>申请单号</th><th>服务类别</th><th>申请单描述</th><th>申请单状态</th><th>当前处理人</th><th>提交人</th><th>提交时间</th></tr>");
            $.each(data.Data.list, function (i, item) {
                var Status;
                switch (item.stauts) {
                    case 1:
                        Status = "审批中";
                        break;
                    case 2:
                        Status = "等待中";
                        break;
                    case 3:
                        Status = "处理中";
                        break;
                    case 4:
                        Status = "已完成";
                        break;
                    case 5:
                        Status = "已拒绝";
                        break;
                    case 6:
                        Status = "已取消";
                        break;

                }
                //var str = "#List tr:eq(" + i + ")";
                var sername = item.serviceType.serviceName.toString();
                if (sername.length > 10) {
                    sername = sername.substring(0, 8) + "..";
                }
                var desc = "";
                if (item.description.toString().length > 0) {
                    if (item.description.toString().length > 20) {
                        desc += item.description.toString().substring(0, 17);
                        desc += "...";
                    }
                    else {
                        desc = item.description.toString();
                    }
                }
                else {
                    if (item.BDDescription.toString().length > 20) {
                        desc += item.BDDescription.toString().substring(0, 17);
                        desc += "...";
                    }
                    else {
                        desc = item.BDDescription.toString();
                    }
                }
                if (item.CurrentProcessor != null && item.CurrentProcessor.length > 0) {
                    var CurrPro = "";
                    if (item.stauts != 4 && item.stauts != 5 && item.stauts != 6) {
                        CurrPro = item.CurrentProcessor;
                    }
                    else {
                        CurrPro = "无";
                    }
                }
                else {
                    CurrPro = "无";
                }
                $("<tr class=\"AlternatingRowStyle\"><td>" + item.serviceRequestID + "</td><td>" + sername + "</td><td>" + desc + "</td><td>" + Status + "</td><td>" + CurrPro + "</td><td>" + item.account.userName + "</td><td>" + ConvertJSONDateToJSDateObject(item.createTime) + "</td></tr>").insertAfter($("#List tr:eq(" + i + ")"));

            });
            document.getElementById('itemCount').value = data.Data.count;
            if (stype == 1) {
                var pager = new PagerView('pager');
                pager.index = parseInt($("input#index").val(), 10);
                pager.itemCount = data.Data.count;
                pager.size = parseInt($("input#size").val(), 10);
                pager.onclick = function (index) {
                    $("input#index").val(index);
                    getDoneWorkList(2);
                };
                pager.render();
            }
        }
    });
}